<?php

namespace App\DesignPatterns\Strategy;

use App\Models\Recharge;
use App\Models\Property;
use Cloud;
use Exception;
use DB;
use App\Library\Orders;
/**
 * 法币交易订单取消流程
 */
class CancelHandle implements HandleInterface
{
    public function handle($uid, $rid)
    {
        $orders_model = new Orders\Orders();
        $cid = $orders_model->get_standard();//获取本位币USDT  id
        $rech = Recharge::find($rid);
        //已完成订单不能取消
        if ($rech->status == 2) {
            //订单信息不匹配
            return respF(4008);
        }
        if ($rech->status == 5) {
            return respF(4006);
        }

        /*if ($rech->uid != $uid) {
            //return response()->json(['status' => 0, 'msg' => '订单信息不匹配']);
            return respF(3004);
        }*/
        try {
            //开启事务
            DB::beginTransaction();
            //如果有取消订单的其他限制，在上面分别做判断
            $rech->update(['status' => 4]);
            //如果是用户卖出，解冻用户资产
            if ($rech->type == 2) {
                $property = Property::where(['uid' => $rech->uid, 'cid' => $cid])->first();
                $property->increment('balance', $rech->num);
                $property->decrement('blocked', $rech->num);
            }
            DB::commit();
            return response()->json(['status' => 1, 'msg' => '提交成功']);
        } catch (Exception $e) {
            DB::rollback();
            return response()->json(['status' => 0, 'msg' => '网络错误001']);

        }

















    }
}